<template>
  <div class="swiper">
    <!-- swiper -->
    <swiper :options="swiperOption" v-if="showswiperList">
      <!-- slides -->
      <swiper-slide v-for="(item, index) in swiperList" :key="index">
        <img class="swiper-img" :src="item.imgUrl" />
      </swiper-slide>
      <!-- Optional controls -->
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
  </div>
</template>
<script>
import axios from "axios";
import {mapState} from 'vuex'
export default {
  data() {
    return {
      lastCity:'',
      swiperOption: {
        pagination: ".swiper-pagination",
        loop: true
      },
      swiperList: []
    };
  },
  created() {
    this.lastCity = this.city
    this.getList();
  },
  methods: {
    getList() {
      axios.get("/api/index.json?city3=" + this.city).then(res => {
        this.swiperList = res.data.data.swiperList;
      });
    }
  },
  // 计算属性
  computed: {
    showswiperList() {
      return this.swiperList.length;
    },
    ...mapState(['city'])
  },
  activated(){
    if(this.lastCity !== this.city){
      this.lastCity = this.city
      this.getList()
    }
  }
};
</script>
<style scoped>
.swiper {
  width: 100%;
  height: 0;
  overflow: hidden;
  padding-bottom: 26.8%;
  /* height: 26.8vw; */
}
.swiper .swiper-img {
  width: 100%;
}
.swiper >>> .swiper-pagination-bullet-active {
  background: #ffffff;
}
</style>